<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>商品分类分类</title>
		<link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
		<link rel="stylesheet" href="__PEAR__/admin/css/other/department.css"/>
	</head>
	<body class="pear-container">

		<div class="layui-row layui-col-space15">
			<div class="layui-col-md3">
				<div class="layui-card">
					<div class="layui-card-body">
						<div id="clsTreeContent" style="overflow: auto">
							<ul id="clsTree" class="dtree organizationTree"></ul>
						</div>
					</div>
				</div>
			</div>
			<div class="layui-col-md9">
				<div class="layui-card">
					<div class="layui-card-body">
						<table id="itemscls-table" lay-filter="itemscls-table"></table>
					</div>
				</div>
			</div>
		</div>

		<script type="text/html" id="itemscls-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
			<i class="layui-icon layui-icon-add-1"></i>
			新增
		</button>
	</script>
	
		<script type="text/html" id="displayup">
			{{ d.display_flag == 1 ? '显示' : '隐藏' }}
		</script>

		<script type="text/html" id="itemscls-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
			<i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
			<i class="layui-icon layui-icon-delete"></i></button>
	    </script>
		<script src="__STATIC__/lib/layui/layui.js"></script>
		<script src="__PEAR__/component/pear/pear.js"></script>
		<script src="__STATIC__/js/admin/common.js"></script>
		<script>
			var _parent="";
			
			layui.use(['table', 'form', 'jquery', 'dtree'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let dtree = layui.dtree;
				
				_parent='';
				
				let MODULE_PATH = "/{$MODULE_NAME}/Itemcls/";
				let DATA_PATH = MODULE_PATH+"dataList";
				let TREE_PATH="/{$MODULE_NAME}/imsheet/Jsondata/ItemClass";

				let cols = [
					[
						{
							title: '分类编号',
							field: 'item_clsno',
							align: 'center'
						},
						{
							title: '分类名称',
							field: 'item_clsname',
							align: 'center'
						},
						{
							title: '上级分类编码',
							field: 'cls_parent',
							align: 'center'
						},
						{
							title: '是否显示',
							field: 'display_flag',
							align: 'center',
							templet: '#displayup'
						},
						{
							title: '排序',
							field: 'orderby',
							align: 'center'
						},
						{
							title: '操作',
							toolbar: '#itemscls-bar',
							align: 'center',
							width: 130
						}
					]
				]
				
				var treeHeight=parent.globalHeight-25;
				var DTree = dtree.render({
					elem: "#clsTree",
					height:treeHeight,
					initLevel: "2", //默认展开层级为1
					line: true, // 有线树
					ficon: ["1", "-1"], // 设定一级图标样式。0表示方形加减图标，8表示小圆点图标
					icon: ["0", "0"], // 设定二级图标样式。0表示文件夹图标，5表示叶子图标
					method: 'get',
					url: TREE_PATH
				});

				table.render({
					elem: '#itemscls-table',
					url: DATA_PATH,
					height: 'full-50',
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#itemscls-toolbar',
					defaultToolbar: []
				});

				// 绑定节点点击事件
				dtree.on("node(clsTree)", function(obj) {
					_parent=obj.param.nodeId;
					
					//console.log(obj.param.nodeId);
					
					//layer.msg("正在获取数据");
					//table.reload("itemscls-table",{
							//where:{'parent':_parent}
					//});
					
				});

				table.on('tool(itemscls-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(itemscls-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(itemscls-query)', function(data) {
					table.reload('itemscls-table', {
						where: data.field,
						page:{curr:1}
					})
					return false;
				});

				window.add = function() {
					//if(_parent==0||_parent==''){
						//layer.msg("请选择上级分类");
						//return;
					//}
					layer.open({
						type: 2,
						title: '新增商品分类',
						shade: 0.1,
						area: ['800px', '400px'],
						content: MODULE_PATH + 'clsAdd/parent/'+_parent+'.html'
					});
				}
				
				window.edit = function(obj) {
					var item_clsno=obj.data.item_clsno;
					layer.open({
						type: 2,
						title: '编辑商品分类',
						shade: 0.1,
						area: ['800px', '400px'],
						content: MODULE_PATH + 'clsAdd/itemcls/'+item_clsno+'.html'
					});
				}

				window.remove = function(obj) {
					
					layer.confirm('确定要删除该商品分类?', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "clsDel/item_clsno/" + obj.data['item_clsno'],
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
									var treeHeight=getParentWinHeight($)-25;
									rebuildTree(dtree,"clsTree",TREE_PATH,treeHeight);
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				
				window.refresh = function(param) {
					table.reload('itemscls-table');
				}

				$(document).ready(function(){
					layer.msg("添加下级分类先点击左侧分类",{time:3000});
				})
				//$(window).resize(function(){
					//var treeHeight=getParentWinHeight($)-25;
					//rebuildTree(dtree,"clsTree",TREE_PATH,treeHeight);
				//})
				
			})
		</script>
	</body>
</html>
